package com.ebeijia.service.wechat.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
import me.chanjar.weixin.mp.bean.WxMpCustomMessage.WxArticle;
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ebeijia.service.wechat.ITemplateMessageService;

/**
 * Created by zb on 2015/6/24.
 */

@Service
public class TemplateMessageService implements ITemplateMessageService {

    @Autowired
    private WxMpService wxMpService;

    // @Value("\${template.msg.pay.success.first}")
    private String firstData;

    // @Value("\${template.msg.pay.fail.first}")
    private String failData;

    // @Value("\${template.msg.pay.success.remark}")
    private String remarkData;

    // @Value("\${template.msg.pay.fail.remark}")
    private String remarkFailData;

    // @Value("\${template.msg.id}")
    private String templateId;

    // @Value("\${template.msgfail.id}")
    private String templateFailId;

    // @Value("\${template.msgBasic.url}")
    private String templateBasicUrl;

    final Logger logger = LoggerFactory.getLogger(this.getClass());

    public TemplateMessageService() {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("global");

        this.firstData = resourceBundle.getString("template.msg.pay.success.first");

        this.failData = resourceBundle.getString("template.msg.pay.fail.first");

        this.remarkData = resourceBundle.getString("template.msg.pay.success.remark");

        this.remarkFailData = resourceBundle.getString("template.msg.pay.fail.remark");

        this.templateId = resourceBundle.getString("template.msg.id");

        this.templateFailId = resourceBundle.getString("template.msgfail.id");

        this.templateBasicUrl = resourceBundle.getString("template.msgBasic.url");
    }

    @Override
    public void sendSuccessPayMsg(String openId, String remark, String payTime, String payType, String payFee,
        String url) {
        try {
            logger.info("发送成功模板消息：获取到的消息地址：" + url + " openId:" + openId + " templateId:" + templateId);
            WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
            templateMessage.setToUser(openId);
            templateMessage.setTemplateId(templateId);
            templateMessage.setUrl(url);
            List<WxMpTemplateData> datas = new ArrayList<WxMpTemplateData>();
            datas.add(new WxMpTemplateData("first", firstData.replaceAll("remark", remark)));
            datas.add(new WxMpTemplateData("JFSJ", payTime));
            datas.add(new WxMpTemplateData("JFFS", payType));
            datas.add(new WxMpTemplateData("JFJE", payFee));
            datas.add(new WxMpTemplateData("REMARK", remark + "\n" + remarkFailData));

            String msgId = wxMpService.templateSend(templateMessage);

            logger.info("发送模板消息结束，返回消息id:" + msgId);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

    }

    @Override
    public void sendFailPayMsg(String openId, String remark, String payTime, String payType, String payFee,
        String url) {

        try {
            logger.info("发送失败模板消息：获取到的消息地址：" + url + " openId:" + openId + " templateId:" + templateFailId);
            WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
            templateMessage.setToUser(openId);
            templateMessage.setTemplateId(templateFailId);
            templateMessage.setUrl(url);
            List<WxMpTemplateData> datas = new ArrayList<WxMpTemplateData>();
            datas.add(new WxMpTemplateData("first", failData.replaceAll("remark", remark)));
            datas.add(new WxMpTemplateData("keyword1", payTime));
            datas.add(new WxMpTemplateData("keyword2", payType));
            datas.add(new WxMpTemplateData("keyword3", payFee));
            datas.add(new WxMpTemplateData("remark", remark + "\n" + remarkFailData));

            String msgId = wxMpService.templateSend(templateMessage);

            logger.info("发送模板消息结束，返回消息id:" + msgId);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }

    }

    @Override
    public void sendNewsMsg(String openId, WxArticle wxArticle) {
        try {
            WxMpCustomMessage message = WxMpCustomMessage.NEWS().toUser(openId).addArticle(wxArticle).build();
            wxMpService.customMessageSend(message);
        } catch (WxErrorException e) {
            logger.error(e.getMessage(), e);
        }
    }

    @Override
    public void sendUnpaidBillMsg(String templateUnpaidBillId, String templateUnpaidBillFirst, String openId,
        String billType, String needPay, String day, String billStatus, String url, String templateUnpaidBillRemark) {
        try {
            WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
            templateMessage.setToUser(openId);
            templateMessage.setTemplateId(templateUnpaidBillId);
            templateMessage.setUrl(url);
            List<WxMpTemplateData> datas = new ArrayList<WxMpTemplateData>();
            datas.add(new WxMpTemplateData("first", templateUnpaidBillFirst));
            datas.add(new WxMpTemplateData("keyword1", billType));
            datas.add(new WxMpTemplateData("keyword2", needPay));
            datas.add(new WxMpTemplateData("keyword3", day));
            datas.add(new WxMpTemplateData("keyword4", billStatus));
            datas.add(new WxMpTemplateData("remark", templateUnpaidBillRemark));
            wxMpService.templateSend(templateMessage);
        } catch (Exception e) {
            logger.error("向用户发送未缴费账单异常:", e);
        }
    }

    @Override
    public void sendBillInfoMsg(String openId,String templateUnpaidBillId, String templateUnpaidBillFirst, String remark,
         String address, String billAmt, String waterCons) {
        try {
            WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
            templateMessage.setToUser(openId);
            templateMessage.setTemplateId(templateUnpaidBillId);
            List<WxMpTemplateData> datas = new ArrayList<>();
            datas.add(new WxMpTemplateData("first", templateUnpaidBillFirst));
            datas.add(new WxMpTemplateData("keyword1", address));
            datas.add(new WxMpTemplateData("keyword2", waterCons));
			datas.add(new WxMpTemplateData("keyword3",billAmt));
			datas.add(new WxMpTemplateData("remark",remark));
			wxMpService.templateSend(templateMessage);
        } catch (Exception e) {
            logger.error("向用户推送账单异常", e);
        }
    }

    @Override
    public void sendTextMsg(String openId, String msg) {
        try {
            WxMpCustomMessage message = WxMpCustomMessage.TEXT().toUser(openId).content(msg).build();
            wxMpService.customMessageSend(message);
        } catch (WxErrorException e) {
            logger.error(e.getMessage(), e);

        }

    }
}
